//
//  ViewController.m
//  LOLerBox
//
//  Created by 杨智超 on 16/1/15.
//  Copyright © 2016年 蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
#import <UIImageView+WebCache.h>
#import "YZCAFNetwork.h"
#import "YZCConsultModel.h"
#import "YZCConsultCollectionViewCell.h"
#import "YZCNewsWebViewViewController.h"
#import "CycleImage.h"
#import "YZCCycleViewController.h"
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)NSMutableArray *dataArr;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UISegmentedControl *seg;
@property (nonatomic, retain)NSIndexPath *indexPath;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
    [self createCollectionView];
    [self createSegmented];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"新闻资讯";
    
    
    
}


/* 创建collectionView */
- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    /* 横向滑动 */
    layout.scrollDirection =UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(375, 667 - 64 - 50 - 49);
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64 + 50, 375, 667 - 49 - 64 - 50) collectionViewLayout:layout];
    
    /* 整页滑动 */
    [self.collectionView setPagingEnabled:YES];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    /* 取消边缘弹动 */
    self.collectionView.bounces = NO;
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    [layout release];
    [self.collectionView registerClass:[YZCConsultCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

/* collectionView协议方法 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

/* cell设置 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
     YZCConsultCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
       
        cell.str = @"http://lol.zhangyoubao.com/apis/rest/ItemsService/lists?catid=10178&cattype=news&page=1&i_=A000004850FCE4&t_=1452762319984&p_=32297&v_=400704&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=21&cha=Anzogame&u_=19460314&modle_=SM-N9009";
        
        
        void (^block)() = ^(NSNumber *mid){
            NSLog(@"%@", mid);
            YZCNewsWebViewViewController *webViewVC = [[YZCNewsWebViewViewController alloc] init];
            NSString *str = [NSString stringWithFormat:@"%@", mid];
            webViewVC.url = str;
            [self.navigationController pushViewController:webViewVC animated:YES];
        };
        cell.block = block;
        return cell;
    }
    else if(indexPath.row == 1) {
        cell.str = @"http://lol.zhangyoubao.com/apis/rest/ItemsService/lists?catid=10000&cattype=news&page=1&i_=A000004850FCE4&t_=1452770266345&p_=21380&v_=400704&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=21&cha=Anzogame&u_=19460314&modle_=SM-N9009";
        void (^block)() = ^(NSNumber *mid){
            NSLog(@"%@", mid);
            YZCNewsWebViewViewController *webViewVC = [[YZCNewsWebViewViewController alloc] init];
            NSString *str = [NSString stringWithFormat:@"%@", mid];
            webViewVC.url = str;
            [self.navigationController pushViewController:webViewVC animated:YES];
        };
        cell.block = block;
        return cell;
    }else if(indexPath.row == 2) {
        cell.str = @"http://lol.zhangyoubao.com/apis/rest/ItemsService/lists?catid=10006&cattype=news&page=1&i_=A000004850FCE4&t_=1452770476499&p_=21842&v_=400704&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=21&cha=Anzogame&u_=19460314&modle_=SM-N9009";
        return cell;
    }else{
        cell.str = @"http://lol.zhangyoubao.com/apis/rest/ItemsService/lists?catid=10179&cattype=news&page=1&i_=A000004850FCE4&t_=1452770937945&p_=26180&v_=400704&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=21&cha=Anzogame&u_=19460314&modle_=SM-N9009";
        return cell;
    }
    
    return nil;
}


/* collectionVeiw滚动方法 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.collectionView) {
        if (self.collectionView.contentOffset.x == 375 * 1) {
            self.seg.selectedSegmentIndex = 1;
        }if (self.collectionView.contentOffset.x == 375 * 2){
            self.seg.selectedSegmentIndex = 2;
        }if (self.collectionView.contentOffset.x == 375 * 3) {
            self.seg.selectedSegmentIndex = 3;
        }if (self.collectionView.contentOffset.x == 0) {
            self.seg.selectedSegmentIndex = 0;
        }

    }
}

/* 创建按钮栏 */
- (void)createSegmented {
    self.seg = [[UISegmentedControl alloc]initWithItems:@[@"最新", @"新闻", @"赛事", @"娱乐"]];
    self.seg.frame = CGRectMake(0, 64, 375, 50);
    /* 文字颜色 */
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil];
    [self.seg setTitleTextAttributes:dic forState:UIControlStateNormal];
    /* 点击后颜色 */
    self.seg.tintColor = [UIColor colorWithWhite:0.251 alpha:1.000];
    [self.view addSubview:self.seg];
    self.seg.selectedSegmentIndex = 0;
    /* 背景颜色 */
    self.seg.backgroundColor = [UIColor blackColor];
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [_seg release];
}

/* 按钮栏点击方法 */
- (void)segAction:(UISegmentedControl *)seg {
    /* 点击第几个按钮 */
    NSInteger index = seg.selectedSegmentIndex;
    /* collectionView偏移量 */
    CGFloat set = self.collectionView.contentOffset.x;
    
    switch (index) {
        case 0:
            set = 0;
            break;
            case 1:
            set = 375;
            break;
            case 2:
            set = 375 * 2;
            break;
        default:
            set = 375 * 3;
            break;
    }
    
    [self.collectionView setContentOffset:CGPointMake(set, 0) animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
